package com.example.spring.statmachine.example.listener;

import com.example.spring.statmachine.example.annotation.StateMachineAnno;
import com.example.spring.statmachine.example.constans.CommonConstants;
import com.example.spring.statmachine.example.domain.TbOrder;
import com.example.spring.statmachine.example.enums.OrderStatusChangeEventEnum;
import com.example.spring.statmachine.example.enums.OrderStatusEnum;
import com.example.spring.statmachine.example.mapper.OrderMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.Message;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author zhangwenxue
 */
@Component("orderStateListener")
@WithStateMachine(name = "orderStateMachine")
public class OrderStateListener {

    public static final Logger logger = LoggerFactory.getLogger(OrderStateListener.class);

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private StateMachine<OrderStatusEnum, OrderStatusChangeEventEnum> orderStateMachine;

    @OnTransition(source = "WAIT_PAYMENT", target = "WAIT_DELIVER")
    @StateMachineAnno(key = CommonConstants.PAY_TRANSITION,keyExpression = "#message.headers[order].id")
    public void payTransition(Message<OrderStatusChangeEventEnum> message) {
        TbOrder order = (TbOrder) message.getHeaders().get("order");
        logger.info("支付，状态机反馈信息：{}", message.getHeaders());
        //更新订单
        assert order != null;
        order.setStatus(OrderStatusEnum.WAIT_DELIVER.getKey());
        order.setUpdateTime(new Date());
        orderMapper.updateById(order);
        //TODO 其他业务
    }

    @OnTransition(source = "WAIT_DELIVER", target = "WAIT_RECEIVE")
    @StateMachineAnno(key = CommonConstants.DELIVER_TRANSITION,keyExpression = "#message.headers[order].order.id")
    public void deliverTransition(Message<OrderStatusChangeEventEnum> message) {
        TbOrder order = (TbOrder) message.getHeaders().get("order");
        logger.info("发货，状态机反馈信息：{}", message.getHeaders());
            //更新订单
            assert order != null;
            order.setStatus(OrderStatusEnum.WAIT_RECEIVE.getKey());
            order.setUpdateTime(new Date());
            orderMapper.updateById(order);
            //TODO 其他业务
    }
    @OnTransition(source = "WAIT_RECEIVE", target = "FINISH")
    @StateMachineAnno(key = CommonConstants.RECEIVE_TRANSITION,keyExpression = "#message.headers[order].id")
    public void receiveTransition(Message<OrderStatusChangeEventEnum> message) {
        TbOrder order = (TbOrder) message.getHeaders().get("order");
        logger.info("确认收货，状态机反馈信息：{}", message.getHeaders());
            //更新订单
            assert order != null;
            order.setStatus(OrderStatusEnum.FINISH.getKey());
            order.setUpdateTime(new Date());
            orderMapper.updateById(order);
            //TODO 其他业务
    }


}
